/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.controller;

import com.seari.bean.User;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryUser;
import com.seari.service.UserService;
import com.seari.utils.StringHelper;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Rainbow
 */
@Controller
@RequestMapping("user/")
public class UserController
{
    @Autowired
    private UserService userService;
    @RequestMapping("getUserByLogin")
    @ResponseBody
    public MessageBean getUserByLogin(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String stationName = req.getParameter("stationName");
        if(StringUtils.isBlank(stationName))
        {
        	//默认为OCC站进行登陆
        	stationName = "occ";
        }
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("用户名或密码为空");
            return mb;
        }
        User user = userService.getUserByLogin(userName, password, stationName);
        if(null != user && user.getUid() > 0 )
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setData(user);
        }else
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("用户密码不匹配或无此用户");
        }
        return  mb;
    }
    
    @RequestMapping("getUserByParams")
    @ResponseBody
    public DataGridBean<User> getUserByParams(HttpServletRequest req)
    {
        DataGridBean<User> grid = new DataGridBean<User>();
        String userName = req.getParameter("userName");
        String stationName = req.getParameter("stationName");
        QueryUser qu = new QueryUser();
        if(StringUtils.isNotBlank(userName))
        {
            qu.setUserName(userName);
        }
        if(StringUtils.isNotBlank(stationName))
        {
            qu.setStationName(stationName);
        }
        List<User> users = userService.getUserByParams(qu);
        grid.setRows(users);
        grid.setTotal(users.size());
        return  grid;
    }
    
    @RequestMapping("addUser")
    @ResponseBody
    public MessageBean addUser(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String stationName = req.getParameter("stationName");
        String furid = req.getParameter("urid");
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(stationName) || StringUtils.isBlank(furid))
        {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }
        QueryUser qu = new QueryUser();
        qu.setUserName(userName);
        qu.setStationName(stationName);
        int userCount = userService.checkUserExist(qu);
        if(userCount < 1)
        {
            //检查是否已存在此用户
            User user = new User();
            user.setUserName(userName);
            user.setPassword(password);
            user.setStationName(stationName);
            user.setFurid(Integer.parseInt(furid));
            int resultNum = userService.addUser(user);
            if(resultNum > 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("添加成功");
            }
            else
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("添加失败");
            }
        }else{
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("用户已存在");
        }
        return  mb;
    }
    
    @RequestMapping("updateUser")
    @ResponseBody
    public MessageBean updateUser(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String uid = req.getParameter("uid");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String stationName = req.getParameter("stationName");
        String furid = req.getParameter("urid");
        if(StringUtils.isBlank(uid) || StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(stationName) || StringUtils.isBlank(furid))
        {
            //表单验证
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }
        QueryUser qu = new QueryUser();
        qu.setUid(Integer.parseInt(uid));
        qu.setUserName(userName);
        qu.setStationName(stationName);
        int userCount = userService.checkUserExist(qu);
        if(userCount <1 )
        {
            //检查是否已存在此用户
            User user  = new User();
            user.setUid(Long.parseLong(uid));
            user.setUserName(userName);
            user.setPassword(password);
            user.setStationName(stationName);
            user.setFurid(Integer.parseInt(furid));
            int resultNum = userService.updateUser(user);
            if(resultNum > 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("用户更新成功");
            }
            else
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("用户更新失败");
            }
        }
        else
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("用户名已存在");
        }
        return mb;
    }
    
    @RequestMapping("deleteUser")
    @ResponseBody
    public MessageBean deleteUser(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String uid = req.getParameter("uid");
        if(StringUtils.isBlank(uid))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("用户ID缺失");
            return mb;
        }
        int resultNum = userService.deleteUser(Integer.parseInt(uid));
        if(resultNum < 0)
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("删除用户失败");
        }
        else{
            mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            mb.setMessage("删除用户成功");
        }
        return mb;
    }
}
